package com.ocean.service.edu.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ocean.dao.edu.StudentDao;
import com.ocean.entity.edu.Student;
import com.ocean.entity.user.User;
import com.ocean.service.edu.StudentService;
import com.ocean.vo.R;
import org.apache.poi.hpsf.Array;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.naming.BinaryRefAddr;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author greenbird
 * @create 2023-03-04 15:47
 */
@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentDao dao;

    @Override
    public R save(Student student) {
        if(dao.insert(student)>0){
            return R.ok();
        }else {
            return R.fail();
        }
    }

    @Override
    public R delete(int id) {
        if(dao.delete(id)>0){
            return R.ok();
        }else {
            return R.fail();
        }
    }

    @Override
    public R update(Student student) {
        if(dao.update(student)>0){
            return R.ok();
        }else {
            return R.fail();
        }
    }

    @Override
    public R queryPage(String sname,String sex,String classid,int page,int size) {
        PageHelper.startPage(page, size);
        PageInfo<Student> pageInfo=new PageInfo<>(dao.selectWhere(sname,sex,classid));
        return R.ok(pageInfo);
    }

    @Override
    public Student queryById(Integer id) {
        return dao.queryById(id);
    }



    @Override
    public R importData(MultipartFile file) {
        //1.验证上传的内容是否非空
        if(!file.isEmpty()){

            try {
                //2.EasyExcel读取---集合
                List<Student> list=EasyExcel.read(file.getInputStream(), Student.class, new ReadListener() {
                    @Override
                    public void invoke(Object o, AnalysisContext analysisContext) {
                    }
                    @Override
                    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
                    }
                }).doReadAllSync();

                System.out.println(list);
                //3.批量新增
                if(dao.insertBatch(list)>0){
                    return R.ok();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return R.fail();
    }

    @Override
    public void exportData(HttpServletResponse response) {
        //1.设置下载的响应消息头
        response.setHeader("Content-Disposition", "attachment;filename=student_" + System.currentTimeMillis()+".xlsx");
        //2.查询数据
        List<Student> list=dao.selectAll();
        //3.生成Excel 同时写出
        try {
            EasyExcel.write(response.getOutputStream(),Student.class).sheet("自动导出").doWrite(list);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public R selectIdByName(String name) {
        return R.ok(dao.selectIdByName(name));
    }
}
